package de.htw.ejb.transaktionen.pizzaverwaltung.schnittstelle;

import java.util.Collection;
import java.util.Properties;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import de.htw.ejb.manager.EntityManagementDienstRemote;
import de.htw.ejb.manager.exceptions.EntityManagementDienstException;
import de.htw.ejb.model.Pizza;
import de.htw.ejb.model.Zutat;


@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class Pizzaverwaltung implements IPizzaverwaltung {
	
	EntityManagementDienstRemote em;
	Properties properties = new Properties();
	
	
	public void initManager(){
		properties.put( Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory" );
		properties.put( Context.PROVIDER_URL, "127.0.0.1:1099" );
		InitialContext ctx;
		try{
		ctx = new InitialContext( properties );
		
		em = ( EntityManagementDienstRemote ) ctx
			.lookup( "EntityManagementDienstBean/remote" );
		}catch ( NamingException e1 )
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	

	@Override
	public void erstellePizza(String name, int preis, Collection<Zutat> zutaten) {
		initManager();
		Pizza neu= new Pizza();
		neu.setBezeichnung(name);
		neu.setKategorie(1);
		neu.setPreis(preis);
		neu.setZutaten(zutaten);
		try {
			em.speichern(neu);
		} catch (EntityManagementDienstException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	

	}

	@Override
	public void loeschePizza(String pizzaname) {
		initManager();
		Pizza alt=suchePizza(pizzaname);
		try {
			em.loeschePizza(alt.getArtikelnummer());
		} catch (EntityManagementDienstException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	

	}

	@Override
	public void aenderePreis(String pizzaname, double neuerPreis) {
		initManager();
		Pizza alt=suchePizza(pizzaname);
		loeschePizza(pizzaname);
		alt.setPreis(neuerPreis);
		try {
			em.speichern(alt);
		} catch (EntityManagementDienstException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		// TODO Auto-generated method stub

	}

	@Override
	public void loescheZutat(String pizzaname, Zutat zutat) {
		initManager();
		Pizza alte=suchePizza(pizzaname);
		loeschePizza(pizzaname);
		alte.loeschenZutat(zutat);
		try {
			em.speichern(alte);
		} catch (EntityManagementDienstException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub

	}

	@Override
	public void fuegeZutathinzu(String pizzaname, Zutat zutat) {
		initManager();
		Pizza alte=suchePizza(pizzaname);
		loeschePizza(pizzaname);
		alte.hinzufuegenZutat(zutat);
		try {
			em.speichern(alte);
		} catch (EntityManagementDienstException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub

	}

	@Override
	public Pizza suchePizza(String pizzaname) {
		initManager();
		Pizza suchPizza=null;
		try {
			suchPizza = (Pizza) em.suchePizzaPerBezeichnung(pizzaname);
		} catch (EntityManagementDienstException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suchPizza;
	}

	@Override
	public Pizza suchePizza(int pizzaid) {
		initManager();
		Pizza suchPizza=null;
		try {
			suchPizza = (Pizza) em.suchePizzaPerArtikelnummer(pizzaid);
		} catch (EntityManagementDienstException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suchPizza;
	}

}
